﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Pages_SicoNide_Shipping : System.Web.UI.Page
{


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            handleEditMode();
            BindShippingType();
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (ValidateForm() != "")
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", "alert('" + ValidateForm() + "');", true);
            return;
        }
        if (btnSave.CommandArgument == "")
        {
            tbl_siconide_shipping model = new tbl_siconide_shipping();
            model.Drum_Number = ddlDrum_Number.SelectedValue;
            model.Net_Weight_Shipped = Convert.ToDecimal(txtNet_Weight_Shipped.Text.Trim());
            model.Gross_Weight_Shipped = Convert.ToDecimal(txtGross_Weight_Shipped.Text.Trim());
            model.Shipping_Date = txtShipping_Date.Text.Trim().toDateTimeWithoutError();
            model.PO_Number = ddlPO_Number.SelectedValue;
            model.Shipping_Method = ddlShipping_Method.SelectedValue;
            model.Shipping_Vessel = txtShipping_Vessel.Text.Trim();
            model.CREATED_BY = User.Identity.Name; ;
            model.CREATED_DATE = DateTime.Now;



            BLL_tbl_siconide_shipping bll = new BLL_tbl_siconide_shipping();
            try
            {



                bll.insert(model);
                string script = "top.location=" + '"' + HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + Request.RawUrl + '"';
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Success", "alert('Record Saved Succesfully');", true);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Postback", script, true);

                return;
            }
            catch (Exception)
            {

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", "alert('Error Occured');", true);
                return;
            }
        }
        else
        {
            tbl_siconide_shipping model = new tbl_siconide_shipping();
            model.ID = Convert.ToInt32(btnSave.CommandArgument);
            model.Drum_Number = ddlDrum_Number.SelectedValue;
            model.Net_Weight_Shipped = Convert.ToDecimal(txtNet_Weight_Shipped.Text.Trim());
            model.Gross_Weight_Shipped = Convert.ToDecimal(txtGross_Weight_Shipped.Text.Trim());
            model.Shipping_Date = txtShipping_Date.Text.Trim().toDateTimeWithoutError();
            model.PO_Number = ddlPO_Number.SelectedValue;
            model.Shipping_Method = ddlShipping_Method.SelectedValue;
            model.Shipping_Vessel = txtShipping_Vessel.Text.Trim();
            model.UPDATED_BY = User.Identity.Name;
            model.UPDATED_DATE = DateTime.Now;



            BLL_tbl_siconide_shipping bll = new BLL_tbl_siconide_shipping();
            bll.update(model);
            btnSave.CommandArgument = "";
            Response.Redirect(Request.Url.AbsolutePath);
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.Url.AbsolutePath);
    }

    protected void linkDelete_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        tbl_siconide_shipping model = new tbl_siconide_shipping();
        model.ID = Convert.ToInt32(id); ;

        BLL_tbl_siconide_shipping bll = new BLL_tbl_siconide_shipping();
        bll.delete(model);
    }

    protected void linkEdit_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        Response.Redirect(Request.Url.AbsolutePath + "?editRecord=" + id);
    }


    protected void handleEditMode()
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["editRecord"] != null)
            {
                string id = Request.QueryString["editRecord"];
                BLL_tbl_siconide_shipping bll = new BLL_tbl_siconide_shipping();
                List<tbl_siconide_shipping> list = bll.select(Convert.ToInt32(id));
                foreach (tbl_siconide_shipping tbl in list)
                {
                    btnSave.CommandArgument = tbl.ID.ToString();
                    ddlDrum_Number.SelectedValue = tbl.Drum_Number;
                    txtNet_Weight_Shipped.Text = tbl.Net_Weight_Shipped.ToString();
                    txtGross_Weight_Shipped.Text = tbl.Gross_Weight_Shipped.ToString();
                    txtShipping_Date.Text = tbl.Shipping_Date.ToString("yyyy-MM-dd");
                    ddlPO_Number.SelectedValue = tbl.PO_Number;
                    ddlShipping_Method.SelectedValue = tbl.Shipping_Method;
                    txtShipping_Vessel.Text = tbl.Shipping_Vessel;
                    

                }
            }
        }
    }


 
    protected void ddlPO_Number_DataBound(object sender, EventArgs e)
    {
        ListItem li = new ListItem("--Select--", "0");
        ddlPO_Number.Items.Insert(0, li);
    }


    #region Shipping Type
    private void BindShippingType()
    {
        BLL_tbl_purchase_order bll = new BLL_tbl_purchase_order();
        try
        {
            DataTable dt = bll.GetShippingType();
            ddlShipping_Method.DataSource = dt;
            ddlShipping_Method.DataTextField = dt.Columns["SHIPPING_TYPE"].ToString();
            ddlShipping_Method.DataValueField = dt.Columns["SHIPPING_ID"].ToString();
            ddlShipping_Method.DataBind();
            ddlShipping_Method.Items.Insert(0, "--Select Shipping Type--");
            return;
        }
        catch (Exception)
        {

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", "alert('Error Occured while fetching Client Details');", true);
            return;
        }
        finally
        {
            bll = null;

        }
    }

    #endregion

    #region Validate Form

    private string ValidateForm()
    {
        string str = "";
        try
        {
            // Check if Client is Selected 
            if (ddlDrum_Number.SelectedIndex == 0) return "Please Select Drum Number ";
            if (txtNet_Weight_Shipped.Text == "") return "Please enter Net Weight to Be shipped ";
            if (txtGross_Weight_Shipped.Text == "") return "Please enter Gross Weight to Be shipped";


            if (txtShipping_Date.Text == "") return "Please enter Shipping Date";
            if (ddlPO_Number.SelectedIndex == 0) return "Please Select PO Number";
            if (ddlShipping_Method.SelectedIndex == 0) return "Please Select Shipping Method";

            if (txtShipping_Vessel.Text == "") return "Please enter Shipping Vessel";

            return str;
        }
        catch (Exception)
        {

            return "Error in Validation";
        }
        finally
        {
            str = null;
        }
    }

    #endregion
}